Introduction:
In this article, we are going to explain what is subquery in SQL or how to create subquery in SQL or how to use a subquery in SQL server with the example.
Description:
A subquery is a SQL query within a query. Subqueries are nested queries that provide data to the enclosing query. Subqueries can return individual values or a list of records, Subqueries must be enclosed with round brackets.
There is no general syntax; subqueries are regular queries placed inside round brackets. Subqueries can be used in different ways and at different locations inside a query:
Here is a subquery with the IN operator.
Let’s see simply subquery in SQL Server
Customer Table
OrderDetails Table
Subquery with the IN operator.
SELECT ID,NAME,ADDRESS1 FROM CUSTOMER
WHERE ID IN (SELECT ID FROM orderDetails WHERE AMOUNT=10000)
In above example, we have a customer table and orderdetails table, we have created a subquery which is return customer details, which customer order amount equal 10000.
OutPut:
i hope it will help you after reading it.
you might also like to read next topic create a simple cursor in sql server, how to import excel data in sql server database, Types Of Join in Sql Server, Union Example in SQL Server, Trigger in SQL
Leave Comment